﻿using DocumentFormat.OpenXml.Spreadsheet;
using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using LimaLims.Application.Service.TestCenter.Dto;
using LimaLims.Core;
using LimaLims.Core.Entity.Basic;
using LimaLims.Core.Entity.Experiment;
using LimaLims.Core.Entity.TestCenter;
using LimaLims.Core.Enum;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Linq;

namespace LimaLims.Application.Service.TestCenter
{
    /// <summary>
    /// 试验数据
    /// </summary>
    [ApiDescriptionSettings(Name = "Experiment", Order = 167)]
    [Route("api")]
    public class ExperimentService : IExperimentService, IDynamicApiController, ITransient
    {
        private readonly IRepository<TransformationRatio> _transformationRatioRep;  // 变比试验
        private readonly IRepository<DcResistance> _dcResistanceRep;//直阻试验
        private readonly IRepository<LoadTest> _loadTestRep;//负载试验
        private readonly IRepository<NoLoadTest> _noLoadTestRep;//空载试验
        private readonly IRepository<ChromTest> _chromTestRep;//绝缘油色谱试验
        private readonly IRepository<InsulationResistance> _insulationResistanceRep;//绝缘电阻试验
        private readonly IRepository<AcsdTest> _acsdTestRep;//感应耐压试验查询分页
        private readonly IRepository<PfwvTest> _pfwvRep;//工频耐压试验查询分页

        public ExperimentService(IRepository<TransformationRatio> transformationRatioRep, IRepository<DcResistance> dcResistanceRep, IRepository<LoadTest> loadTestRep, IRepository<NoLoadTest> noLoadTestRep, IRepository<ChromTest> chromTestRep, IRepository<InsulationResistance> insulationResistanceRep, IRepository<AcsdTest> acsdTestRep, IRepository<PfwvTest> pwdvTestRep)
        {
            _transformationRatioRep = transformationRatioRep;
            _dcResistanceRep = dcResistanceRep;
            _loadTestRep = loadTestRep;
            _noLoadTestRep = noLoadTestRep;
            _chromTestRep = chromTestRep;
            _insulationResistanceRep = insulationResistanceRep;
            _acsdTestRep = acsdTestRep;
            _pfwvRep = pwdvTestRep;
        }

        /// <summary>
        /// 删除试验数据
        /// </summary>
        [HttpPost("experiment/delete")]
        [UnitOfWork]
        public async Task Delete(long id, InspectionType type)
        {
            switch (type)
            {
                case InspectionType.DcResistance:
                    var dcResistance = await _dcResistanceRep.FirstOrDefaultAsync(s => s.ExperimentTaskId == id);
                    if (dcResistance == null)
                        throw Oops.Oh("不存在此数据！");
                    await dcResistance.DeleteAsync();
                    break;
                case InspectionType.TransformationRatio:
                    var transformationRatio = await _transformationRatioRep.FirstOrDefaultAsync(s => s.ExperimentTaskId == id);
                    if (transformationRatio == null)
                        throw Oops.Oh("不存在此数据！");
                    await transformationRatio.DeleteAsync();
                    break;
                case InspectionType.LoadTest:
                    var loadTest = await _loadTestRep.FirstOrDefaultAsync(s => s.ExperimentTaskId == id);
                    if (loadTest == null)
                        throw Oops.Oh("不存在此数据！");
                    await loadTest.DeleteAsync();
                    break;
                case InspectionType.NoLoadTest:
                    var noLoadTest = await _noLoadTestRep.FirstOrDefaultAsync(s => s.ExperimentTaskId == id);
                    if (noLoadTest == null)
                        throw Oops.Oh("不存在此数据！");
                    await noLoadTest.DeleteAsync();
                    break;
                case InspectionType.InsulationResistance:
                    var insulationResistance = await _insulationResistanceRep.FirstOrDefaultAsync(s => s.ExperimentTaskId == id);
                    if (insulationResistance == null)
                        throw Oops.Oh("不存在此数据！");
                    await insulationResistance.DeleteAsync();
                    break;
                case InspectionType.AcsdTest:
                    var acsdTest = await _acsdTestRep.FirstOrDefaultAsync(s => s.ExperimentTaskId == id);
                    if (acsdTest == null)
                        throw Oops.Oh("不存在此数据！");
                    await acsdTest.DeleteAsync();
                    break;
                case InspectionType.PfwvTest:
                    var pfwvTest = await _pfwvRep.FirstOrDefaultAsync(s => s.ExperimentTaskId == id);
                    if (pfwvTest == null)
                        throw Oops.Oh("不存在此数据！");
                    await pfwvTest.DeleteAsync();
                    break;
                case InspectionType.ChromTest:
                    var chromTest = await _chromTestRep.FirstOrDefaultAsync(s => s.ExperimentTaskId == id);
                    if (chromTest == null)
                        throw Oops.Oh("不存在此数据！");
                    await chromTest.DeleteAsync();
                    break;
                default:
                    throw Oops.Oh("试验类型参数有误！");
                    break;
            }
        }


        /// <summary>
        /// 查看试验数据
        /// </summary>
        [HttpGet("experiment/detail")]
        [UnitOfWork]
        public async Task<object> Detail(long id, InspectionType? type)
        {
            if (type == null)
            {
                throw Oops.Oh("试验类型参数有误！");
            }

            if (type == InspectionType.DcResistance)
            {
                var dcResistance = await _dcResistanceRep.FirstOrDefaultAsync(s => s.ExperimentTaskId == id);
                if (dcResistance == null)
                    throw Oops.Oh("不存在此数据！");
                return dcResistance;
            }
            else if (type == InspectionType.TransformationRatio)
            {
                var transformationRatio = await _transformationRatioRep.FirstOrDefaultAsync(s => s.ExperimentTaskId == id);
                if (transformationRatio == null)
                    throw Oops.Oh("不存在此数据！");
                return transformationRatio;
            }
            else if (type == InspectionType.LoadTest)
            {
                var loadTest = await _loadTestRep.DetachedEntities.Include(s => s.ExperimentTask).Include(s => s.EquipmentInfo)
                    .Where(s => s.ExperimentTaskId == id).Select(u=> new LoadTestOutput
                {
                    Id = u.Id,
                    FactoryNumbe = u.FactoryNumbe,
                    EquipmentInfoId = u.EquipmentInfo.Id,
                    EquipmentCode = u.EquipmentInfo.Code,
                    EquipmentName = u.EquipmentInfo.Name,
                    ExperimentTaskCode = u.ExperimentTask.TaskCode,
                    ExperimentTaskId = u.ExperimentTaskId,
                    AmbientHumidity = u.AmbientHumidity,
                    AmbientTemperature = u.AmbientTemperature,
                    TappingGear = u.TappingGear,
                    UrmsAB = u.UrmsAB,
                    UrmsBC = u.UrmsBC,
                    UrmsCA = u.UrmsCA,
                    UrmsAverage = u.UrmsAverage,
                    IrmsAB = u.IrmsAB,
                    IrmsBC = u.IrmsBC,
                    IrmsCA = u.IrmsCA,
                    IrmsAverage = u.IrmsAverage,
                    IsSinglePhase = u.IsSinglePhase,
                    Frequency = u.Frequency,
                    PAB = u.PAB,
                    PBC = u.PBC,
                    PCA = u.PCA,
                    SigmaP = u.SigmaP,
                    Pk75 = u.Pk75,
                    Uk75 = u.Uk75,
                    Zk75 = u.Zk75,
                    PowerFactor = u.PowerFactor,
                    Result = u.Result,
                    CreatedTime = u.CreatedTime,
                    CreatedUserName = u.CreatedUserName
                }).FirstOrDefaultAsync();

                if (loadTest == null)
                    throw Oops.Oh("不存在此数据！");
                return loadTest;
            }
            else if (type == InspectionType.NoLoadTest)
            {
                var noLoadTest = await _noLoadTestRep.FirstOrDefaultAsync(s => s.ExperimentTaskId == id);
                if (noLoadTest == null)
                    throw Oops.Oh("不存在此数据！");
                return noLoadTest;
            }
            else if (type == InspectionType.InsulationResistance)
            {
                var insulationResistance = await _insulationResistanceRep.FirstOrDefaultAsync(s => s.ExperimentTaskId == id);
                if (insulationResistance == null)
                    throw Oops.Oh("不存在此数据！");
                return insulationResistance;
            }
            else if (type == InspectionType.AcsdTest)
            {
                var acsdTest = await _acsdTestRep.FirstOrDefaultAsync(s => s.ExperimentTaskId == id);
                if (acsdTest == null)
                    throw Oops.Oh("不存在此数据！");
                return acsdTest;
            }
            else if (type == InspectionType.PfwvTest)
            {
                var pfwvTest = await _pfwvRep.FirstOrDefaultAsync(s => s.ExperimentTaskId == id);
                if (pfwvTest == null)
                    throw Oops.Oh("不存在此数据！");
                return pfwvTest;
            }
            else if (type == InspectionType.ChromTest)
            {
                var chromTest = await _chromTestRep.FirstOrDefaultAsync(s => s.ExperimentTaskId == id);
                if (chromTest == null)
                    throw Oops.Oh("不存在此数据！");
                return chromTest;
            }

            return null;
        }

        /// <summary>
        /// 直阻试验分页查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("experiment/dcResistancePage")]
        public async Task<PageResult<DcResistanceOutput>> DcResistanceQueryPage([FromQuery] DcResistancePageInput input)
        {
            var experimentTaskCode = !string.IsNullOrEmpty(input.ExperimentTaskCode?.Trim());
            var equipmentCode = !string.IsNullOrEmpty(input.EquipmentCode?.Trim());
            var factoryNumbe = !string.IsNullOrEmpty(input.FactoryNumbe?.Trim());

            var items = await _dcResistanceRep.DetachedEntities.Include(s => s.ExperimentTask).Include(s => s.EquipmentInfo)
                                         .Where((experimentTaskCode, u => EF.Functions.Like(u.ExperimentTask.TaskCode, $"%{input.ExperimentTaskCode.Trim()}%")))
                                         .Where((equipmentCode, u => EF.Functions.Like(u.EquipmentInfo.Code, $"%{input.EquipmentCode.Trim()}%")))
                                         .Where((factoryNumbe, u => EF.Functions.Like(u.FactoryNumbe, $"%{input.FactoryNumbe.Trim()}%")))
                                         .OrderByDescending(u => u.CreatedTime).ThenBy(s => s.TappingGear)
                                         .Select(u => new DcResistanceOutput
                                         {
                                             Id = u.Id,
                                             FactoryNumbe = u.FactoryNumbe,
                                             EquipmentInfoId = u.EquipmentInfo.Id,
                                             EquipmentCode = u.EquipmentInfo.Code,
                                             EquipmentName = u.EquipmentInfo.Name,
                                             ExperimentTaskCode = u.ExperimentTask.TaskCode,
                                             ExperimentTaskId = u.ExperimentTaskId,
                                             AmbientHumidity = u.AmbientHumidity,
                                             AmbientTemperature = u.AmbientTemperature,
                                             WindingMethod = u.WindingMethod,
                                             TappingGear = u.TappingGear,
                                             HighAB = u.HighAB,
                                             HighBC = u.HighBC,
                                             HighCA = u.HighCA,
                                             HighUnbalanceRate = u.HighUnbalanceRate,
                                             High0UnbalanceRate = u.High0UnbalanceRate,
                                             HighA0 = u.HighA0,
                                             HighB0 = u.HighB0,
                                             HighC0 = u.HighC0,
                                             Lowab = u.Lowab,
                                             Lowbc = u.Lowbc,
                                             Lowca = u.Lowca,
                                             Lowa0 = u.Lowa0,
                                             Lowb0 = u.Lowb0,
                                             Lowc0 = u.Lowc0,
                                             Low0UnbalanceRate = u.Low0UnbalanceRate,
                                             LowUnbalanceRate = u.LowUnbalanceRate,
                                             Result = u.Result,
                                             CreatedTime = u.CreatedTime,
                                             CreatedUserName = u.CreatedUserName
                                         }).ToADPagedListAsync(input.PageNo, input.PageSize);

            return items;
        }

        /// <summary>
        /// 绝缘电阻试验分页查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("experiment/insulationResistance")]
        public async Task<PageResult<InsulationResistanceOutput>> InsulationResistanceQueryPage([FromQuery] InsulationResistancePageInput input)
        {
            var experimentTaskCode = !string.IsNullOrEmpty(input.ExperimentTaskCode?.Trim());
            var equipmentCode = !string.IsNullOrEmpty(input.EquipmentCode?.Trim());
            var factoryNumbe = !string.IsNullOrEmpty(input.FactoryNumbe?.Trim());
            var items = await _insulationResistanceRep.DetachedEntities.Include(s => s.ExperimentTask).Include(s => s.EquipmentInfo)
                                         .Where((experimentTaskCode, u => EF.Functions.Like(u.ExperimentTask.TaskCode, $"%{input.ExperimentTaskCode.Trim()}%")))
                                         .Where((equipmentCode, u => EF.Functions.Like(u.EquipmentInfo.Code, $"%{input.EquipmentCode.Trim()}%")))
                                          .Where((factoryNumbe, u => EF.Functions.Like(u.FactoryNumbe, $"%{input.FactoryNumbe.Trim()}%")))
                                         .OrderByDescending(u => u.CreatedTime)
                                         .Select(u => new InsulationResistanceOutput
                                         {
                                             Id = u.Id,
                                             FactoryNumbe = u.FactoryNumbe,
                                             EquipmentInfoId = u.EquipmentInfo.Id,
                                             EquipmentCode = u.EquipmentInfo.Code,
                                             EquipmentName = u.EquipmentInfo.Name,
                                             ExperimentTaskCode = u.ExperimentTask.TaskCode,
                                             ExperimentTaskId = u.ExperimentTaskId,
                                             AmbientHumidity = u.AmbientHumidity,
                                             AmbientTemperature = u.AmbientTemperature,
                                             R15 = u.R15,
                                             R60 = u.R60,
                                             TestType = u.TestType,
                                             TestVoltage = u.TestVoltage,
                                             Position = u.Position,
                                             AbsorptionRatio = u.AbsorptionRatio,
                                             Result = u.Result,
                                             CreatedTime = u.CreatedTime,
                                             CreatedUserName = u.CreatedUserName
                                         }).ToADPagedListAsync(input.PageNo, input.PageSize);

            return items;
        }

        /// <summary>
        /// 负载试验分页查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("experiment/loadTestPage")]
        public async Task<PageResult<LoadTestOutput>> LoadTestQueryPage([FromQuery] LoadTestPageInput input)
        {
            var experimentTaskCode = !string.IsNullOrEmpty(input.ExperimentTaskCode?.Trim());
            var equipmentCode = !string.IsNullOrEmpty(input.EquipmentCode?.Trim());
            var factoryNumbe = !string.IsNullOrEmpty(input.FactoryNumbe?.Trim());
            var items = await _loadTestRep.DetachedEntities.Include(s => s.ExperimentTask).Include(s => s.EquipmentInfo)
                                         .Where((experimentTaskCode, u => EF.Functions.Like(u.ExperimentTask.TaskCode, $"%{input.ExperimentTaskCode.Trim()}%")))
                                         .Where((equipmentCode, u => EF.Functions.Like(u.EquipmentInfo.Code, $"%{input.EquipmentCode.Trim()}%")))
                                        .Where((factoryNumbe, u => EF.Functions.Like(u.FactoryNumbe, $"%{input.FactoryNumbe.Trim()}%")))
                                         .OrderByDescending(u => u.CreatedTime).ThenBy(s => s.TappingGear)
                                         .Select(u => new LoadTestOutput
                                         {
                                             Id = u.Id,
                                             FactoryNumbe   =u.FactoryNumbe,
                                             EquipmentInfoId = u.EquipmentInfo.Id,
                                             EquipmentCode = u.EquipmentInfo.Code,
                                             EquipmentName = u.EquipmentInfo.Name,
                                             ExperimentTaskCode = u.ExperimentTask.TaskCode,
                                             ExperimentTaskId = u.ExperimentTaskId,
                                             AmbientHumidity = u.AmbientHumidity,
                                             AmbientTemperature = u.AmbientTemperature,
                                             TappingGear = u.TappingGear,
                                             UrmsAB = u.UrmsAB,
                                             UrmsBC = u.UrmsBC,
                                             UrmsCA = u.UrmsCA,
                                             UrmsAverage = u.UrmsAverage,
                                             IrmsAB = u.IrmsAB,
                                             IrmsBC = u.IrmsBC,
                                             IrmsCA = u.IrmsCA,
                                             IrmsAverage = u.IrmsAverage,
                                             IsSinglePhase = u.IsSinglePhase,
                                             Frequency = u.Frequency,
                                             PAB = u.PAB,
                                             PBC = u.PBC,
                                             PCA = u.PCA,
                                             SigmaP = u.SigmaP,
                                             Pk75 = u.Pk75,
                                             Uk75 = u.Uk75,
                                             Zk75 = u.Zk75,
                                             PowerFactor = u.PowerFactor,
                                             Result = u.Result,
                                             CreatedTime = u.CreatedTime,
                                             CreatedUserName = u.CreatedUserName
                                         }).ToADPagedListAsync(input.PageNo, input.PageSize);

            return items;
        }

        /// <summary>
        /// 绝缘油色谱试验分页查询
        /// </summary>
        /// <returns></returns>
        [HttpGet("experiment/chromTestPage")]
        public async Task<PageResult<ChromTestOutput>> ChromTestQueryPage([FromQuery] ChromTestPageInput input)
        {
            var experimentTaskCode = !string.IsNullOrEmpty(input.ExperimentTaskCode?.Trim());
            var equipmentCode = !string.IsNullOrEmpty(input.EquipmentCode?.Trim());
            var items = await _chromTestRep.DetachedEntities.Include(s => s.ExperimentTask).Include(s => s.EquipmentInfo)
                                         .Where((experimentTaskCode, u => EF.Functions.Like(u.ExperimentTask.TaskCode, $"%{input.ExperimentTaskCode.Trim()}%")))
                                         .Where((equipmentCode, u => EF.Functions.Like(u.EquipmentInfo.Code, $"%{input.EquipmentCode.Trim()}%")))
                                         .OrderByDescending(u => u.CreatedTime)
                                         .Select(u => new ChromTestOutput
                                         {
                                             Id = u.Id,
                                             EquipmentInfoId = u.EquipmentInfo.Id,
                                             EquipmentCode = u.EquipmentInfo.Code,
                                             EquipmentName = u.EquipmentInfo.Name,
                                             ExperimentTaskCode = u.ExperimentTask.TaskCode,
                                             ExperimentTaskId = u.ExperimentTaskId,
                                             AmbientHumidity = u.AmbientHumidity,
                                             AmbientTemperature = u.AmbientTemperature,
                                             O2 = u.O2,
                                             CO = u.CO,
                                             C2H2 = u.C2H2,
                                             C2H4 = u.C2H4,
                                             C2H6 = u.C2H6,
                                             CH4 = u.CH4,
                                             CO2 = u.CO2,
                                             SamplingDate = u.SamplingDate,
                                             DiagnoseResult = u.DiagnoseResult,
                                             H2 = u.H2,
                                             N2 = u.N2,
                                             Voc = u.Voc,
                                             TestDate = u.TestDate,
                                             Memo = u.Memo,
                                             Result = u.Result,
                                             CreatedTime = u.CreatedTime,
                                             CreatedUserName = u.CreatedUserName
                                         }).ToADPagedListAsync(input.PageNo, input.PageSize);

            return items;
        }

        /// <summary>
        /// 空载试验分页查询
        /// </summary>
        /// <returns></returns>
        [HttpGet("experiment/noLoadTestPage")]
        public async Task<PageResult<NoLoadTestOutput>> NoLoadTestQueryPage([FromQuery] NoLoadTestPageInput input)
        {
            var experimentTaskCode = !string.IsNullOrEmpty(input.ExperimentTaskCode?.Trim());
            var equipmentCode = !string.IsNullOrEmpty(input.EquipmentCode?.Trim());
            var factoryNumbe = !string.IsNullOrEmpty(input.FactoryNumbe?.Trim());
            var items = await _noLoadTestRep.DetachedEntities.Include(s => s.ExperimentTask).Include(s => s.EquipmentInfo)
                                         .Where((experimentTaskCode, u => EF.Functions.Like(u.ExperimentTask.TaskCode, $"%{input.ExperimentTaskCode.Trim()}%")))
                                         .Where((equipmentCode, u => EF.Functions.Like(u.EquipmentInfo.Code, $"%{input.EquipmentCode.Trim()}%")))
                                         .Where((factoryNumbe, u => EF.Functions.Like(u.FactoryNumbe, $"%{input.FactoryNumbe.Trim()}%")))
                                         .OrderByDescending(u => u.CreatedTime).ThenBy(s => s.TappingGear)
                                         .Select(u => new NoLoadTestOutput
                                         {
                                             Id = u.Id,
                                             FactoryNumbe=u.FactoryNumbe,
                                             EquipmentInfoId = u.EquipmentInfo.Id,
                                             EquipmentCode = u.EquipmentInfo.Code,
                                             EquipmentName = u.EquipmentInfo.Name,
                                             ExperimentTaskCode = u.ExperimentTask.TaskCode,
                                             ExperimentTaskId = u.ExperimentTaskId,
                                             AmbientHumidity = u.AmbientHumidity,
                                             AmbientTemperature = u.AmbientTemperature,
                                             TappingGear = u.TappingGear,
                                             UrmsAB = u.UrmsAB,
                                             UrmsBC = u.UrmsBC,
                                             UrmsCA = u.UrmsCA,
                                             UrmsAverage = u.UrmsAverage,
                                             IrmsAB = u.IrmsAB,
                                             IrmsBC = u.IrmsBC,
                                             IrmsCA = u.IrmsCA,
                                             IrmsAverage = u.IrmsAverage,
                                             IsSinglePhase = u.IsSinglePhase,
                                             Frequency = u.Frequency,
                                             PAB = u.PAB,
                                             PBC = u.PBC,
                                             PCA = u.PCA,
                                             SigmaP = u.SigmaP,
                                             I0 = u.I0,
                                             P0 = u.P0,
                                             KF = u.KF,
                                             UrecAB = u.UrecAB,
                                             UrecBC = u.UrecBC,
                                             UrecCA = u.UrecCA,
                                             UrecAverage = u.UrecAverage,
                                             PowerFactor = u.PowerFactor,
                                             Result = u.Result,
                                             CreatedTime = u.CreatedTime,
                                             CreatedUserName = u.CreatedUserName
                                         }).ToADPagedListAsync(input.PageNo, input.PageSize);

            return items;
        }

        /// <summary>
        /// 变比试验分页查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("experiment/transformationRatioPage")]
        public async Task<PageResult<TransformationRatioOutput>> TransformationRatioQueryPage([FromQuery] TransformationRatioPageInput input)
        {
            var experimentTaskCode = !string.IsNullOrEmpty(input.ExperimentTaskCode?.Trim());
            var equipmentCode = !string.IsNullOrEmpty(input.EquipmentCode?.Trim());
            var factoryNumbe = !string.IsNullOrEmpty(input.FactoryNumbe?.Trim());
            var items = await _transformationRatioRep.DetachedEntities.Include(s => s.ExperimentTask).Include(s => s.EquipmentInfo)
                                         .Where(input.ExperimentTaskId.HasValue, u => u.ExperimentTaskId == input.ExperimentTaskId)
                                         .Where((experimentTaskCode, u => EF.Functions.Like(u.ExperimentTask.TaskCode, $"%{input.ExperimentTaskCode.Trim()}%")))
                                         .Where((equipmentCode, u => EF.Functions.Like(u.EquipmentInfo.Code, $"%{input.EquipmentCode.Trim()}%")))
                                         .Where((factoryNumbe, u => EF.Functions.Like(u.FactoryNumbe, $"%{input.FactoryNumbe.Trim()}%")))
                                         .OrderByDescending(u => u.CreatedTime).ThenBy(s => s.TappingGear)
                                         .Select(u => new TransformationRatioOutput
                                         {
                                             Id = u.Id,
                                             FactoryNumbe= u.FactoryNumbe,
                                             EquipmentInfoId = u.EquipmentInfo.Id,
                                             EquipmentCode = u.EquipmentInfo.Code,
                                             EquipmentName = u.EquipmentInfo.Name,
                                             ExperimentTaskCode = u.ExperimentTask.TaskCode,
                                             ExperimentTaskId = u.ExperimentTaskId,
                                             AmbientHumidity = u.AmbientHumidity,
                                             AmbientTemperature = u.AmbientTemperature,
                                             TappingGear = u.TappingGear,
                                             WiringMode = u.WiringMode,
                                             AngleAb = u.AngleAb,
                                             AngleBc = u.AngleBc,
                                             AngleCa = u.AngleCa,
                                             DeviationA = u.DeviationA,
                                             DeviationB = u.DeviationB,
                                             DeviationC = u.DeviationC,
                                             HighAB = u.HighAB,
                                             HighBC = u.HighBC,
                                             HighCA = u.HighCA,
                                             Lowab = u.Lowab,
                                             Lowbc = u.Lowbc,
                                             Lowca = u.Lowca,
                                             TurnsRatioA = u.TurnsRatioA,
                                             TurnsRatioB = u.TurnsRatioB,
                                             TurnsRatioC = u.TurnsRatioC,
                                             Result = u.Result,
                                             CreatedTime = u.CreatedTime,
                                             CreatedUserName = u.CreatedUserName
                                         }).ToADPagedListAsync(input.PageNo, input.PageSize);

            return items;
        }

        /// <summary>
        /// 感应耐压试验查询分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("experiment/acsdTest")]
        public async Task<PageResult<AcsdTestOutput>> AcsdTestQueryPage([FromQuery] AcsdTestPageInput input)
        {
            var experimentTaskCode = !string.IsNullOrEmpty(input.ExperimentTaskCode?.Trim());
            var equipmentCode = !string.IsNullOrEmpty(input.EquipmentCode?.Trim());
            var factoryNumbe = !string.IsNullOrEmpty(input.FactoryNumbe?.Trim());
            var items = await _acsdTestRep.DetachedEntities.Include(s => s.ExperimentTask).Include(s => s.EquipmentInfo)
                                         .Where((experimentTaskCode, u => EF.Functions.Like(u.ExperimentTask.TaskCode, $"%{input.ExperimentTaskCode.Trim()}%")))
                                         .Where((equipmentCode, u => EF.Functions.Like(u.EquipmentInfo.Code, $"%{input.EquipmentCode.Trim()}%")))
                                         .Where((factoryNumbe, u => EF.Functions.Like(u.FactoryNumbe, $"%{input.FactoryNumbe.Trim()}%")))
                                         .OrderByDescending(u => u.CreatedTime)
                                         .Select(u => new AcsdTestOutput
                                         {
                                             Id = u.Id,
                                             FactoryNumbe = u.FactoryNumbe,
                                             EquipmentInfoId = u.EquipmentInfo.Id,
                                             EquipmentCode = u.EquipmentInfo.Code,
                                             EquipmentName = u.EquipmentInfo.Name,
                                             ExperimentTaskCode = u.ExperimentTask.TaskCode,
                                             ExperimentTaskId = u.ExperimentTaskId,
                                             AmbientHumidity = u.AmbientHumidity,
                                             AmbientTemperature = u.AmbientTemperature,
                                             AtmosphericPressure = u.AtmosphericPressure,
                                             Frequency = u.Frequency,
                                             HighVoltage = u.HighVoltage,
                                             LowVoltage = u.LowVoltage,
                                             PressurizedPart = u.PressurizedPart,
                                             TestTime = u.TestTime,
                                             Result = u.Result,
                                             CreatedTime = u.CreatedTime,
                                             CreatedUserName = u.CreatedUserName
                                         }).ToADPagedListAsync(input.PageNo, input.PageSize);

            return items;
        }

        /// <summary>
        /// 工频耐压试验查询分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("experiment/pfwvTest")]
        public async Task<PageResult<PfwvTestOutput>> PfwvTestQueryPage([FromQuery] PfwvTestPageInput input)
        {
            var experimentTaskCode = !string.IsNullOrEmpty(input.ExperimentTaskCode?.Trim());
            var equipmentCode = !string.IsNullOrEmpty(input.EquipmentCode?.Trim());
            var factoryNumbe = !string.IsNullOrEmpty(input.FactoryNumbe?.Trim());
            var items = await _pfwvRep.DetachedEntities.Include(s => s.ExperimentTask).Include(s => s.EquipmentInfo)
                                         .Where((experimentTaskCode, u => EF.Functions.Like(u.ExperimentTask.TaskCode, $"%{input.ExperimentTaskCode.Trim()}%")))
                                         .Where((equipmentCode, u => EF.Functions.Like(u.EquipmentInfo.Code, $"%{input.EquipmentCode.Trim()}%")))
                                         .Where((factoryNumbe, u => EF.Functions.Like(u.FactoryNumbe, $"%{input.FactoryNumbe.Trim()}%")))
                                         .OrderByDescending(u => u.CreatedTime)
                                         .Select(u => new PfwvTestOutput
                                         {
                                             Id = u.Id,
                                             FactoryNumbe = u.FactoryNumbe,
                                             EquipmentInfoId = u.EquipmentInfo.Id,
                                             EquipmentCode = u.EquipmentInfo.Code,
                                             EquipmentName = u.EquipmentInfo.Name,
                                             ExperimentTaskCode = u.ExperimentTask.TaskCode,
                                             ExperimentTaskId = u.ExperimentTaskId,
                                             AmbientHumidity = u.AmbientHumidity,
                                             AmbientTemperature = u.AmbientTemperature,
                                             AtmosphericPressure = u.AtmosphericPressure,
                                             PressurizedPart = u.PressurizedPart,
                                             TestTime = u.TestTime,
                                             TestVoltage = u.TestVoltage,
                                             Result = u.Result,
                                             CreatedTime = u.CreatedTime,
                                             CreatedUserName = u.CreatedUserName
                                         }).ToADPagedListAsync(input.PageNo, input.PageSize);

            return items;
        }
    }
}
